﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using Telerik.WinControls.UI;
using System.Data.SqlClient;
using System.IO;


namespace PlanningDepartment
{
    public partial class Fr_HuMan_LoadData : Telerik.WinControls.UI.RadForm
    {
        public Fr_HuMan_LoadData()
        {
            InitializeComponent();
        }
        private void ChangeThemeName(Control control, string themeName)
        {
            IComponentTreeHandler radControl = control as IComponentTreeHandler;
            if (radControl != null) { radControl.ThemeName = themeName; }
            foreach (Control child in control.Controls)
            { ChangeThemeName(child, themeName); }
        }

        private void Fr_HuMan_LoadData_Load(object sender, EventArgs e)
        {
            ChangeThemeName(this, "Office2010Blue");
        }

        private void bt_Browse_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            // Lấy đường dẫn file import vừa chọn
            txt_path.Text = ofd.ShowDialog() == DialogResult.OK ? ofd.FileName : "";
        }

        private void bt_LoadData_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable dt=new DataTable();
                Process prs = new Process();
                //prs.Excel_into_DataGrid(radGridView2, txt_path.Text);
                dt = prs.ImportExcel_DataGrid(txt_path.Text, "Sheet1");
                radGridView2.DataSource = dt;
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    radGridView2.Columns[i].Width = 100;
                    //MessageBox.Show(radGridView2.Columns[i].Name.ToString());
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void import_Data()
        {
            Process prs = new Process();
            SqlConnection Conn = prs.Connect_HRP();
            Conn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = Conn;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "Ins_BamThe";
            string Ngay = "", MaNV = "",ID="";
            try
            {
                progressBar1.Minimum = 0;
                progressBar1.Maximum = radGridView2.Rows.Count;
                for (int i = radGridView2.Rows.Count-1; i >=0; i--)
                {
                    try
                    {
                        Ngay = (radGridView2.Rows[i].Cells["Event Date"].Value.ToString() + " " + radGridView2.Rows[i].Cells["Event Time"].Value.ToString()).Replace("'", "");
                        MaNV = radGridView2.Rows[i].Cells["UserID"].Value.ToString().Replace("'","");
                        //ID = MaNV + Ngay;
                        //cmd.Parameters.AddWithValue("@ID", ID);
                        cmd.Parameters.AddWithValue("@MaNV", MaNV);
                        cmd.Parameters.AddWithValue("@EventDate", Ngay);
                        cmd.ExecuteNonQuery();
                        cmd.Parameters.Clear();
                        radGridView2.Rows.RemoveAt(i);
                        progressBar1.Value = i;
                        //radLabel1.Text = i.ToString();
                    }
                    catch 
                    {
                       
                    }
                    //string_temp = radGridView2.Rows[i].Cells[0].Value.ToString()+ radGridView2.Rows[i].Cells[1].Value.ToString().Replace("'", " ") + "'";
                }
                MessageBox.Show("Dữ liệu được lưu thành công");
                //string tem
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                cmd.Dispose();
                Conn.Close();
            }
        }

        private void bt_addSystem_Click(object sender, EventArgs e)
        {
            import_Data();
        }

    }
}
